6
6
.
.
7
7
V
V
i
i
e
e
w
w
s
s
-
-
C
C
o
o
n
n
t
t
a
a
i
i
n
n
e
e
r
r
I
I
n
n
f
f
o
o
Container Views (Parent) are used to put other Views (Children) inside them: Visible, Container and Helper Views.
This way you can organize Child Views on the Screen by using different Container View alignment functionalities.
Their Syntax combines following Swift functionalities: Trailing Closure, Function Builder, TupleView<T>.
S
S
w
w
i
i
f
f
t
t
S
S
y
y
n
n
t
t
a
a
x
x
Container View (like VStack) is just a struct whose Constructor accepts
two input parameters with default values (so you can omit them inside parameter list ())
Trailing Closure as last parameter (so it can be given outside parameter list () inside {} brackets)
Provided Closure doesn't return any value. Instead it will be used as a Builder Function.
For Builder Function return value is calculated for each line and these are then combined into what function returns.
In the case of VStack each line returns a single View which are then added to a TupleView which is returned.
In the below example content Closure returns TupleView<Text, Text>.
Simplified Syntax (using default values and Closure outside parameter list)
struct ContentView: View {
var body: some View {
VStack {
Text("Hello")
Text("World")
}
}
}
Full Syntax (specifying values and Closure inside parameter list)
struct ContentView: View {
var body: some View {
VStack(alignment: .leading, spacing: 200, content: { Text("Hello"); Text("World"); } )
}
}
T
T
y
y
p
p
e
e
s
s
HStack
HStack(alignment: .top, spacing: 200) {
Spacer()
Text("First")
Text("Second")
}
VStack
VStack(alignment: .leading, spacing: 200) {
Spacer()
Text("First")
Text("Second")
}
ZStack
ZStack {
Text("First") .offset(x: -100)
Text("Second").offset(x: 100)
}
List
List {
Text("Stanford Dish")
Text("Edgewood")
}
Divider
VStack {
Text("First Line")
Divider()
Text("Second Line")
}
Spacer
VStack {
Spacer()
Text("First Line")
Spacer()
Text("Second Line")
Spacer()
}
NavigationView
//==========================================================================
// ContentView
//==========================================================================
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Content View")
NavigationLink("Go To Second Screen", destination: SecondScreen(name: "John"))
}
.navigationBarTitle("Content View")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
//==========================================================================
// SecondScreen
//==========================================================================
struct SecondScreen: View {
var name: String
var body: some View {
Text("Hello \(name)").navigationBarTitle("Second Screen")
}
}
Video from URL
import SwiftUI
import AVKit
struct ContentView: View {
var body: some View {
VStack {
player().frame(height: UIScreen.main.bounds.height / 3)
Spacer()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct player: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<player>) ->
AVPlayerViewController {
let url = "https://ivoronline.com/BachataBasic.mp4"
let player1 = AVPlayer(url: URL(string: url)!)
let controller = AVPlayerViewController()
controller.player = player1
return controller
}
public func updateUIViewController(_ uiViewController: AVPlayerViewController, context:
UIViewControllerRepresentableContext<player>) {
}
}
Video from local File
let url = Bundle.main.url(forResource: "Pexels", withExtension: "mp4")
let player1 = AVPlayer(url: url!)